package com.wtwd.campus.vcom.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonArray;
import com.wtwd.campus.entity.FencePoints;
import com.wtwd.campus.entity.PolygonFence;
import com.wtwd.campus.service.PolygonFenceService;
import com.wtwd.campus.service.RedisService;
import com.wtwd.campus.utils.DateUtils;
import com.wtwd.campus.vcom.common.Constant;
import com.wtwd.campus.vcom.common.RespCode;
import com.wtwd.campus.vcom.common.RespEntity;
import com.wtwd.campus.vcom.common.ResponseEntity;
import com.wtwd.campus.vcom.dao.VcomFenceMapper;
import com.wtwd.campus.vcom.entity.AddOrgPolygonFenceDto;
import com.wtwd.campus.vcom.entity.VcomCircleFence;
import com.wtwd.campus.vcom.service.VcomFenceService;
import com.wtwd.campus.vcom.utils.RequestIdUtils;
import me.chanjar.weixin.common.util.DataUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Author: ldaoliang
 * @DateTime: 2022/1/7 10:53
 * @Description:
 */
@Service
public class VcomFenceServiceImpl implements VcomFenceService {


    @Resource
    private VcomFenceMapper vcomFenceMapper;
    @Autowired
    private RedisService redisService;

    @Resource
    private PolygonFenceService polygonFenceService;

    @Override
    @Transactional
    public ResponseEntity addOriCircleFence(JSONObject jsonObject, List<String> imeis) {
        String requestId = RequestIdUtils.generateRequestId(redisService, 1);
        VcomCircleFence currentFenceInfo = jsonObject.toJavaObject(VcomCircleFence.class);
        VcomCircleFence dataBaseFenceInfo = vcomFenceMapper.getVcomFenceByFenceId(currentFenceInfo.getFenceId());
        if (dataBaseFenceInfo == null) {
            vcomFenceMapper.addVcomFence(currentFenceInfo);
            vcomFenceMapper.addFenceImei(currentFenceInfo.getFenceId(), imeis);
        } else {
            vcomFenceMapper.updateVcomFence(currentFenceInfo);
            vcomFenceMapper.delFenceImei(currentFenceInfo.getFenceId());
            vcomFenceMapper.addFenceImei(currentFenceInfo.getFenceId(), imeis);
        }
        return new ResponseEntity(RespCode.SUCCESS, true, requestId);
    }

    @Override
    public ResponseEntity incrementAddFenceImei(String fenceId, List<String> imeis) {
        String requestId = RequestIdUtils.generateRequestId(redisService, 1);
        if (imeis == null || imeis.size() == 0) {
            return new ResponseEntity(RespCode.DEVICE_NOT_FOUND, false, requestId);
        }
        VcomCircleFence dataBaseFenceInfo = vcomFenceMapper.getVcomFenceByFenceId(fenceId);
        if (dataBaseFenceInfo == null) {
            PolygonFence polygonFence = polygonFenceService.getOrgPolygonFenceByDeviceTypeAndCustomerIdAndFenceId(Constant.VCOM_DEVICE_TYPE, 1, fenceId);
            if (polygonFence == null) {
                return new ResponseEntity(RespCode.FENCE_NOT_FOUNT, true, requestId);
            }
        }
        int i = vcomFenceMapper.addFenceImei(fenceId, imeis);
        return new ResponseEntity(RespCode.SUCCESS, true, requestId);
    }

    @Override
    public ResponseEntity delFence(String fenceId) {
        String requestId = RequestIdUtils.generateRequestId(redisService, 1);
        PolygonFence polygonFence = polygonFenceService.getOrgPolygonFenceByDeviceTypeAndCustomerIdAndFenceId(Constant.VCOM_DEVICE_TYPE, 1, fenceId);
        if (polygonFence != null) {
            polygonFenceService.deleteById(polygonFence.getId());
        } else {
            vcomFenceMapper.delFence(fenceId);
        }
        vcomFenceMapper.delFenceImei(fenceId);
        return new ResponseEntity(RespCode.SUCCESS, true, requestId);
    }

    @Override
    public ResponseEntity incrementDelFenceImei(String fenceId, List<String> imeis) {
        String requestId = RequestIdUtils.generateRequestId(redisService, 1);
        VcomCircleFence dataBaseFenceInfo = vcomFenceMapper.getVcomFenceByFenceId(fenceId);
        if (dataBaseFenceInfo == null) {
            PolygonFence polygonFence = polygonFenceService.getOrgPolygonFenceByDeviceTypeAndCustomerIdAndFenceId(Constant.VCOM_DEVICE_TYPE, 1, fenceId);
            if (polygonFence == null) {
                return new ResponseEntity(RespCode.FENCE_NOT_FOUNT, true, requestId);
            }
        }
        if (imeis != null) {
            int i = vcomFenceMapper.incrementDelFenceImei(fenceId, imeis);
            return new ResponseEntity(RespCode.SUCCESS, true, requestId);
        }
        return new ResponseEntity(RespCode.SUCCESS, true, requestId);
    }

    @Override
    public RespEntity getFenceInfo(String fenceId) {
        VcomCircleFence dataBaseFenceInfo = vcomFenceMapper.getVcomFenceByFenceId(fenceId);
        if (dataBaseFenceInfo == null) {
            PolygonFence polygonFence = polygonFenceService.getOrgPolygonFenceByDeviceTypeAndCustomerIdAndFenceId(Constant.VCOM_DEVICE_TYPE, 1, fenceId);
            if (polygonFence == null) {
                return new RespEntity(RespCode.FENCE_NOT_FOUNT);
            } else {
                List<String> imeis = vcomFenceMapper.getImeiListByFenceId(fenceId);
                JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(polygonFence));
                jsonObject.put("imeis", imeis);
                jsonObject.put("isActive", jsonObject.getInteger("isActive") == ACTIVE_FENCE ? true : false);
                jsonObject.put("fencePoints", JSONObject.parseArray(jsonObject.getString("fencePoints"), FencePoints.class));
                jsonObject.put("createTime", DateUtils.format(new Date(jsonObject.getLong("createTime")), DateUtils.dateFormat4));
                jsonObject.put("updateTime", DateUtils.format(new Date(jsonObject.getLong("updateTime")), DateUtils.dateFormat4));
                jsonObject.remove("deviceType");
                jsonObject.remove("customerId");
                jsonObject.remove("id");
                return new RespEntity(RespCode.SUCCESS, jsonObject);
            }
        } else {
            List<String> imeis = vcomFenceMapper.getImeiListByFenceId(fenceId);
            JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(dataBaseFenceInfo));
            jsonObject.remove("activeStatus");
            jsonObject.put("imeis", imeis);
            return new RespEntity(RespCode.SUCCESS, jsonObject);
        }
    }

    @Override
    public List<VcomCircleFence> getVcomActiveFenceByImei(String imei) {
        return vcomFenceMapper.getVcomActiveFenceByImei(imei);
    }

    @Override
    public VcomCircleFence getVcomFenceInfoByFenceId(String fenceId) {
        return vcomFenceMapper.getVcomFenceInfoByFenceId(fenceId);
    }

    @Override
    public ResponseEntity addOrgPolygonFence(AddOrgPolygonFenceDto addOrgPolygonFenceDto) {
        String requestId = RequestIdUtils.generateRequestId(redisService, 1);
        PolygonFence polygonFence = polygonFenceService.getOrgPolygonFenceByDeviceTypeAndCustomerIdAndFenceId(Constant.VCOM_DEVICE_TYPE, 1, addOrgPolygonFenceDto.getFenceId());
        if (polygonFence == null) {
            polygonFence = new PolygonFence();
            BeanUtils.copyProperties(addOrgPolygonFenceDto, polygonFence);
            polygonFence.setAlarmType(Integer.valueOf(addOrgPolygonFenceDto.getAlarmType()));
            polygonFence.setFenceType(Integer.valueOf(addOrgPolygonFenceDto.getFenceType()));
            polygonFence.setAppType(Integer.valueOf(addOrgPolygonFenceDto.getAppType()));
            polygonFence.setDeviceType(Constant.VCOM_DEVICE_TYPE);
            polygonFence.setCustomerId(1);
            List<FencePoints> fencePoints = addOrgPolygonFenceDto.getFencePoints();
            String jsonPoints = JSONArray.toJSONString(fencePoints);
            polygonFence.setFencePoints(jsonPoints);
            polygonFence.setIsActive(addOrgPolygonFenceDto.getActive() ? ACTIVE_FENCE : ACTIVE_NOT_FENCE);
            polygonFenceService.save(polygonFence);
        } else {
            BeanUtils.copyProperties(addOrgPolygonFenceDto, polygonFence);
            polygonFence.setAlarmType(Integer.valueOf(addOrgPolygonFenceDto.getAlarmType()));
            polygonFence.setFenceType(Integer.valueOf(addOrgPolygonFenceDto.getFenceType()));
            polygonFence.setAppType(Integer.valueOf(addOrgPolygonFenceDto.getAppType()));
            polygonFence.setDeviceType(Constant.VCOM_DEVICE_TYPE);
            polygonFence.setCustomerId(1);
            polygonFence.setIsActive(addOrgPolygonFenceDto.getActive() ? ACTIVE_FENCE : ACTIVE_NOT_FENCE);
            List<FencePoints> fencePoints = addOrgPolygonFenceDto.getFencePoints();
            String jsonPoints = JSONArray.toJSONString(fencePoints);
            polygonFence.setFencePoints(jsonPoints);
            polygonFenceService.update(polygonFence);
            vcomFenceMapper.delFenceImei(addOrgPolygonFenceDto.getFenceId());
        }
        vcomFenceMapper.addFenceImei(addOrgPolygonFenceDto.getFenceId(), addOrgPolygonFenceDto.getImeis());
        return new ResponseEntity(RespCode.SUCCESS, true, requestId);
    }

    @Override
    public List<PolygonFence> getPolygonFenceByImei(String imei, Integer active) {
        return vcomFenceMapper.getPolygonFenceByImei(imei, active);
    }

}
